package com.example.spring.test;

public class SynchronizedTest2 {

    public static void printA() {
        synchronized (SynchronizedTest2.class) {
            try {
                System.out.println(
                        "线程名称为：" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "进入printA");
                Thread.sleep(3000);
                System.out.println(
                        "线程名称为：" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "离开printA");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    synchronized public static void printB() {
        System.out.println("线程名称为：" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "进入printB");
        System.out.println("线程名称为：" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "离开printB");
    }

    synchronized public void printC() {
        System.out.println("线程名称为：" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "进入printC");
        System.out.println("线程名称为：" + Thread.currentThread().getName() + "在" + System.currentTimeMillis() + "离开printC");
    }


    static class ThreadA extends Thread {
        private SynchronizedTest2 test;

        public ThreadA(SynchronizedTest2 test) {
            this.test = test;
        }

        @Override
        public void run() {
            test.printA();
        }
    }

    static class ThreadB extends Thread {
        private SynchronizedTest2 test;

        public ThreadB(SynchronizedTest2 test) {
            this.test = test;
        }

        @Override
        public void run() {
            test.printB();
        }
    }

    static class ThreadC extends Thread {
        private SynchronizedTest2 test;

        public ThreadC(SynchronizedTest2 test) {
            this.test = test;
        }

        @Override
        public void run() {
            test.printC();
        }
    }

    public static void main(String[] args) {
        SynchronizedTest2 test = new SynchronizedTest2();
        ThreadA a = new ThreadA(test);
        a.setName("A");
        a.start();

        ThreadB b = new ThreadB(test);
        b.setName("B");
        b.start();

        ThreadC c = new ThreadC(test);
        c.setName("C");
        c.start();
    }
}

